package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xiaoxiaode
 * @date 2021/9/25 19:15
 **/
@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl  extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {


    @Autowired
    private ArticleMapper mapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    //@Autowired
    //private FileStorageService fileStorageService;


    private final static short MAX_PAGE_SIZE = 50;

    private final static Integer PAGE_SIZE = 10;

    /*
     * dto:最大时间/最小时间/分页/频道
     * type:1:加载更多  2:加载最新
     *
     * */
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        // 校验参数
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = PAGE_SIZE;
        }
        // 分页的值不超过50(最大分页)
        size = Math.min(size, MAX_PAGE_SIZE);

        // type校验
        if (!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE) && !type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)) {
            type = ArticleConstants.LOADTYPE_LOAD_NEW;
        }

        // 频道校验
        if (org.apache.commons.lang3.StringUtils.isBlank(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        Date date = dto.getMaxBehotTime();
        String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
        System.out.println(time);
        // 时间校验
        if (dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());

        // 查询列表数据
        List<Article> articles = mapper.loadArticleList(dto, type);
        return ResponseResult.okResult(articles);
    }

    /**
     *
     * 保存app端相关文章
     * */
    public ResponseResult saveArticle(ArticleDto dto) {
        // 检查参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle article=new ApArticle();
        BeanUtils.copyProperties(dto,article);
        // 是否存在id
        if(dto.getId() ==null){
            // 保存文章
            save(article);
            // 保存配置
            ApArticleConfig apArticleConfig=new ApArticleConfig(article.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            // 保存文章内容
            ApArticleContent apArticleContent=new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        }else{

            // 修改文章
            updateById(article);

            // 修改文章内容
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());

            apArticleContentMapper.updateById(apArticleContent);
        }

        // 异步调用,生成静态文件道minio中
        articleFreemarkerService.buildArticleToMinio(article,dto.getContent());

        return ResponseResult.okResult(article.getId());

    }

}
